{**
 * Prescription edit
 *
 * @package   OpenEMR
 * @link      http://www.open-emr.org
 * @author    Brady Miller <brady.g.miller@gmail.com>
 * @copyright Copyright (c) 2017 Brady Miller <brady.g.miller@gmail.com>
 * @license   https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
 *}
<html>
<head>

{headerShow}
<link rel="stylesheet" href="{$GLOBALS.assets_static_relative}/bootstrap-3-3-4/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="{$GLOBALS.assets_static_relative}/jquery-ui-1-12-1/themes/base/jquery-ui.min.css" type="text/css" />
<link rel="stylesheet" href="{$GLOBALS.css_header}" type="text/css">

{literal}
<style type="text/css">


input{
   margin: 5px;
}
select{
  margin: 5px;
}
</style>
{/literal}
<script language="Javascript">

{literal}
    function my_process () {
      // Pass the variable
      opener.document.prescribe.drug.value = document.lookup.drug.value;
      // Close the window
      window.self.close();
    }
{/literal}
</script>
{literal}
{/literal}
<!---Gen Look up-->
<script type="text/javascript" src="{$GLOBALS.assets_static_relative}/jquery-min-3-1-1/index.js"></script>
<script src="{$GLOBALS.assets_static_relative}/bootstrap-3-3-4/dist/js/bootstrap.min.js" type="text/javascript"></script>
<script type="text/javascript" src="{$GLOBALS.webroot}/library/dialog.js?v={$GLOBALS.v_js_includes}"></script>
<script type="text/javascript" src="{$GLOBALS.assets_static_relative}/jquery-ui-1-12-1/jquery-ui.js"></script>

{literal}
<script language='JavaScript'>

 // This holds all the default drug attributes.
 // This was html escaped previously
{/literal}
 var drugopts = [{$DRUG_ATTRIBUTES}];
{literal}

 // Helper to choose an option from its value.
 function selchoose(sel, value) {
  var o = sel.options;
  for (i = 0; i < o.length; ++i) {
   o[i].selected = (o[i].value == value);
  }
 }

 // Fill in default values when a drop-down drug is selected.
 function drugselected(sel) {
  var f = document.forms[0];
  var i = f.drug_id.selectedIndex - 1;
  if (i >= 0) {
   var d = drugopts[i];
   f.drug.value = d[0];
   selchoose(f.form, d[1]);
   f.dosage.value = d[2];
   f.size.value = d[3];
   f.rxnorm_drugcode.value = d[11];
   selchoose(f.unit, d[4]);
   selchoose(f.route, d[5]);
   selchoose(f.interval, d[6]);
   selchoose(f.substitute, d[7]);
   f.quantity.value = d[8];
   f.disp_quantity.value = d[8];
   selchoose(f.refills, d[9]);
   f.per_refill.value = d[10];
  }
 }

 // Invoke the popup to dispense a drug.
 function dispense() {
  var f = document.forms[0];
  dlgopen('interface/drugs/dispense_drug.php' +
   {/literal}'?drug_id={$prescription->get_drug_id()}' +{literal}
   '&prescription=' + f.id.value +
   '&quantity=' + f.disp_quantity.value +
   '&fee=' + f.disp_fee.value,
   '_blank', 400, 200);
 }

 function quantityChanged() {
  var f = document.forms[0];
  f.per_refill.value = f.quantity.value;
  if (f.disp_quantity) {
   f.disp_quantity.value = f.quantity.value;
  }
 }

</script>
{/literal}
</head>
<body class="body_top">

<form name="prescribe" id="prescribe"  method="post" action="{$FORM_ACTION}" >
<table>
    <tr><td class="title"><font><b>{xl t='Add'|escape:'html'}/{xl t='Edit'|escape:'html'}</b></font>&nbsp;</td>
    <td><a href=# onclick="submitfun();" class="css_button_small"><span>{xl t='Save'|escape:'html'}</span></a>
    {if $DRUG_ARRAY_VALUES}
    &nbsp; &nbsp; &nbsp; &nbsp;
        {if $prescription->get_refills() >= $prescription->get_dispensation_count()}
            <input type="submit" name="disp_button"class='css_button_small'  style="margin:0 5px 0 2px;" value="{xl t='Save and Dispense'|escape:'html'}" />
            <input class="input-sm" type="text" name="disp_quantity" size="2" maxlength="10" value="{$DISP_QUANTITY|escape:'html'}" />
            units, $
            <input class="input-sm" type="text" name="disp_fee" size="5" maxlength="10" value="{$DISP_FEE|escape:'html'}" />
        {else}&nbsp;
            {xl t='prescription has reached its limit of'|escape:'html'} {$prescription->get_refills()|escape:'html'} {xl t='refills'|escape:'html'}.
        {/if}
    {/if}
         <a class='css_button_small' href="controller.php?prescription&list&id={$prescription->patient->id|escape:'html'}"><span>{xl t='Back'|escape:'html'}</span></a>
</td></tr>
</table>

{if $GLOBALS.enable_amc_prompting}
  <div style='float:right;margin-right:25px;border-style:solid;border-width:1px;'>
    <div style='float:left;margin:5px 5px 5px 5px;'>
      {amcCollect amc_id='e_prescribe_amc' patient_id=$prescription->patient->id object_category='prescriptions' object_id=$prescription->id}
      {if not $amcCollectReturn}
        <input type="checkbox" id="escribe_flag" name="escribe_flag">
      {else}
        <input type="checkbox" id="escribe_flag" name="escribe_flag" checked>
      {/if}
      <span class="text">{xl t='E-Prescription?'|escape:'html'}</span><br>

      {amcCollect amc_id='e_prescribe_chk_formulary_amc' patient_id=$prescription->patient->id object_category='prescriptions' object_id=$prescription->id}
      {if not $amcCollectReturn}
        <input type="checkbox" id="checked_formulary_flag" name="checked_formulary_flag">
      {else}
        <input type="checkbox" id="checked_formulary_flag" name="checked_formulary_flag" checked>
      {/if}
      <span class="text">{xl t='Checked Drug Formulary?'|escape:'html'}</span><br>

      {amcCollect amc_id='e_prescribe_cont_subst_amc' patient_id=$prescription->patient->id object_category='prescriptions' object_id=$prescription->id}
      {if not $amcCollectReturn}
        <input type="checkbox" id="controlled_substance_flag" name="controlled_substance_flag">
      {else}
        <input type="checkbox" id="controlled_substance_flag" name="controlled_substance_flag" checked>
      {/if}
      <span class="text">{xl t='Controlled Substance?'|escape:'html'}</span><br>

    </div>
  </div>
{/if}

<table CELLSPACING="0" CELLPADDING="3" BORDER="0">
<tr>
  <td COLSPAN="1" class="text" ALIGN="right" VALIGN="MIDDLE" >{xl t='Currently Active'|escape:'html'}</td>
  <td COLSPAN="2" ALIGN="LEFT" VALIGN="MIDDLE" >
    <input type="checkbox" name="active" value="1"{if $prescription->get_active() > 0} checked{/if} />
  </td>
</tr>
<tr>
  <td COLSPAN="1" class="text" ALIGN="right" VALIGN="MIDDLE" >{xl t='Starting Date'|escape:'html'}</td>
  <td COLSPAN="2" ALIGN="LEFT" VALIGN="MIDDLE" >
    {html_select_date start_year="-10" end_year="+5" time=$prescription->start_date prefix="start_date_"}
    <input type="hidden" name="start_date" value="{$prescription->start_date|escape:'html'}">
  </td>
</tr>
<tr>
  <td COLSPAN="1" class="text" ALIGN="right" VALIGN="MIDDLE" >{xl t='Provider'|escape:'html'}</td>
  <td COLSPAN="2" ALIGN="LEFT" VALIGN="MIDDLE" >
    {html_options name="provider_id" options=$prescription->provider->utility_provider_array() selected=$prescription->provider->get_id()}
    <input type="hidden" name="patient_id" value="{$prescription->patient->id|escape:'html'}" />
  </td>
</tr>

<tr>
  <td COLSPAN="1" class="text" ALIGN="right" VALIGN="MIDDLE" >{xl t='Drug'|escape:'html'}</td>
  <td COLSPAN="2" ALIGN="LEFT" VALIGN="MIDDLE" >
            <input class="form-control" type="input" size="20" name="drug" id="drug" value="{$prescription->drug|escape:'html'}"/>
            <a href="javascript:;" id="druglookup" class="small" name="B4" onclick="$('#hiddendiv').show(); document.getElementById('hiddendiv').innerHTML='&lt;iframe src=&quot;controller.php?prescription&amp;lookup&amp;drug=&quot; width=&quot;100%&quot;height=&quot;52&quot; scrolling=&quot;no&quot; frameborder=&quot;no&quot;&gt;&lt;/iframe&gt;'">
            ({xl t='click here to search'|escape:'html'})</a>
            <div id=hiddendiv style="display:none">&nbsp;</div>
  </td>
</tr>
{if $DRUG_ARRAY_VALUES}
<tr>
  <td COLSPAN="1" class="text" ALIGN="right" VALIGN="MIDDLE" >&nbsp; {xl t='in-house'|escape:'html'}</td>
  <td COLSPAN="2" ALIGN="LEFT" VALIGN="MIDDLE" >
    <select class="input-sm" name="drug_id" onchange="drugselected(this)">
    {html_options values=$DRUG_ARRAY_VALUES output=$DRUG_ARRAY_OUTPUT selected=$prescription->get_drug_id()}
    </select>
        <input type="hidden" name="rxnorm_drugcode" value="{$prescription->rxnorm_drugcode|escape:'html'}">
  </td>
</tr>
{/if}
<tr>
  <td COLSPAN="1" class="text" ALIGN="right" VALIGN="MIDDLE" >{xl t='Quantity'|escape:'html'}</td>
  <td COLSPAN="2" ALIGN="LEFT" VALIGN="MIDDLE" >
    <input class="input-sm" TYPE="TEXT" NAME="quantity" id="quantity" SIZE="10" MAXLENGTH="31"
     VALUE="{$prescription->quantity|escape:'html'}"
     onchange="quantityChanged()" />
  </td>
</tr>
{if $SIMPLIFIED_PRESCRIPTIONS && !$prescription->size}
<tr style='display:none;'>
{else}
<tr>
{/if}
  <td COLSPAN="1" class="text" ALIGN="right" VALIGN="MIDDLE" >{xl t='Medicine Units'|escape:'html'}</td>
  <td COLSPAN="2" ALIGN="LEFT" VALIGN="MIDDLE" >
    <input class="input-sm" TYPE="TEXT" NAME="size" id="size" SIZE="11" MAXLENGTH="10" VALUE="{$prescription->size|escape:'html'}"/>
    <select class="input-sm" name="unit" id="unit">{html_options options=$prescription->unit_array selected=$prescription->unit}</select>
  </td>
</tr>
<tr>
  <td COLSPAN="1" class="text" ALIGN="right" VALIGN="MIDDLE" >{xl t='Directions'|escape:'html'}</td>
  <td COLSPAN="2" class="text" ALIGN="LEFT" VALIGN="MIDDLE" >
{if $SIMPLIFIED_PRESCRIPTIONS && !$prescription->form && !$prescription->route && !$prescription->interval}
    <input class="input-sm" TYPE="text" NAME="dosage" id="dosage" SIZE="30" MAXLENGTH="100" VALUE="{$prescription->dosage|escape:'html'}" />
    <input type="hidden" name="form" id="form" value="0" />
    <input type="hidden" name="route" id="route" value="0" />
    <input type="hidden" name="interval" id="interval" value="0" />
{else}
    <input class="input-sm" TYPE="TEXT" NAME="dosage" id="dosage" SIZE="2" MAXLENGTH="10" VALUE="{$prescription->dosage|escape:'html'}"/> {xl t='in'|escape:'html'}
    <select class="input-sm" name="form" id="form">{html_options options=$prescription->form_array selected=$prescription->form}</select>
    <select class="input-sm" name="route" id="route">{html_options options=$prescription->route_array selected=$prescription->route}</select>
    <select class="input-sm" name="interval" id="interval">{html_options options=$prescription->interval_array selected=$prescription->interval}</select>
{/if}
  </td>
</tr>
<tr>
  <td COLSPAN="1" class="text" ALIGN="right" VALIGN="MIDDLE" >{xl t='Refills'|escape:'html'}</td>
  <td COLSPAN="2" class="text" ALIGN="LEFT" VALIGN="MIDDLE" >
    {html_options name="refills" options=$prescription->refills_array selected=$prescription->refills}
{if $SIMPLIFIED_PRESCRIPTIONS}
    <input TYPE="hidden" ID="per_refill" NAME="per_refill" VALUE="{$prescription->per_refill|escape:'html'}" />
{else}
    &nbsp; &nbsp; # {xl t='of tablets'}:
    <input class="input-sm" TYPE="TEXT" ID="per_refill" NAME="per_refill" SIZE="2" MAXLENGTH="10" VALUE="{$prescription->per_refill|escape:'html'}" />
{/if}
  </td>
</tr>
<tr>
  <td COLSPAN="1" class="text" ALIGN="right" VALIGN="MIDDLE" >{xl t='Notes'|escape:'html'}</td>
  <td COLSPAN="2" class="text" ALIGN="LEFT" VALIGN="MIDDLE" >
  <textarea class="form-control" name="note" cols="30" rows="2" wrap="virtual">{$prescription->note|escape:'html'}</textarea>
  </td>
</tr>
<tr>
{if $WEIGHT_LOSS_CLINIC}
  <td COLSPAN="1" class="text" ALIGN="right" VALIGN="MIDDLE" >{xl t='Substitution'|escape:'html'}</td>
  <td COLSPAN="2" ALIGN="LEFT" VALIGN="MIDDLE" >
    {html_options name="substitute" options=$prescription->substitute_array selected=$prescription->substitute}
  </td>
{else}
  <td COLSPAN="1" class="text" ALIGN="right" VALIGN="MIDDLE" >{xl t='Add to Medication List'|escape:'html'}</td>
  <td COLSPAN="2" class="text" ALIGN="LEFT" VALIGN="MIDDLE" >
    {html_radios name="medication" options=$prescription->medication_array selected=$prescription->medication}
    &nbsp; &nbsp;
    {html_options name="substitute" options=$prescription->substitute_array selected=$prescription->substitute}
  </td>
{/if}
</tr>
</table>
<input type="hidden" name="id" value="{$prescription->id|escape:'html'}" />
<input type="hidden" name="process" value="{$PROCESS|escape:'html'}" />
<script language='JavaScript'>
{$ENDING_JAVASCRIPT}
</script>
</form>

{literal}
<!-- for the fancy jQuery stuff -->
<script type="text/javascript">

function submitfun() {
    top.restoreSession();
    if (CheckForErrors(this)) {
        document.forms["prescribe"].submit();
    }
    else {
        return false;
    }
}

function iframetopardiv(string){
    var name=string
    document.getElementById('drug').value=name;
    $("#hiddendiv").html( "&nbsp;" );
    $('#hiddendiv').hide();
}

function cancelParlookup () {
    $('#hiddendiv').hide();
    $("#hiddendiv").html( "&nbsp;" );
}

$().ready(function() {

    {/literal}
    {if $GLOBALS.weno_rx_enable}
        {literal}
        $('#drug').autocomplete({
            source: 'library/ajax/drug_autocomplete/search.php',
            minLength: 3
        });
        {/literal}
    {else}
        {literal}
        $('#drug').autocomplete({
            source: 'library/ajax/prescription_drugname_lookup.php',
            minLength: 1
        });
        {/literal}
    {/if}
    {literal}

    $("#drug").focus();
    $("#prescribe").submit(function() { return CheckForErrors(this) });
    $("#druglookup").click(function() { DoDrugLookup(this) });
});


// pop up a drug lookup window with the value of the drug name, if we have one
function DoDrugLookup(eObj) {
    drugname = "";
    if ($('#drug').val() != "") { drugname = $('#drug').val(); }
    $("#druglist").css('display','block');
    document.lookup.action='controller.php?prescription&edit&id=&pid={/literal}{$prescription->patient->id}{literal}&drug=sss'+drugname;
    drugPopup = window.open('controller.php?prescription&lookup&drug='+drugname, 'drugPopup', 'width=400,height=50,menubar=no,titlebar=no,left = 825,top = 400');
    drugPopup.opener = self;
    return true;
}


// check the form for required fields before submitting
var CheckForErrors = function(eObj) {
    // REQUIRED FIELDS
    if (CheckRequired('drug') == false) { return false; }
    if (CheckRequired('quantity') == false) { return false; }
    //if (CheckRequired('unit') == false) { return false; }
    //if (CheckRequired('size') == false) { return false; }
    if (CheckRequired('dosage') == false) { return false; }
    //if (CheckRequired('form') == false) { return false; }
    //if (CheckRequired('route') == false) { return false; }
    //if (CheckRequired('interval') == false) { return false; }

    return top.restoreSession();
};

function CheckRequired(objID) {

    // for text boxes
    if ($('#'+objID).is('input')) {
        if ($('#'+objID).val() == "") {
            alert("{/literal}{xl t='Missing a required field and will be highlighted'}{literal}");
            $('#'+objID).css("backgroundColor", "pink");
            return false;
        }
    }

    // for select boxes
    if ($('#'+objID).is('select')) {
        if ($('#'+objID).val() == "0") {
            alert("{/literal}{xl t='Missing a required field'}{literal}");
            $('#'+objID).css("backgroundColor", "pink");
            return false;
        }
    }

    return true;
}

</script>
{/literal}

</html>
